//
//  XDProductsIntroduceCell.m
//  xindailianmeng
//
//  Created by 张东 on 2017/2/15.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDProductsIntroduceCell.h"

@interface XDProductsIntroduceCell ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleLb;

@property (weak, nonatomic) IBOutlet UILabel *nearBackTimeLb;

@property (weak, nonatomic) IBOutlet UIImageView *nextInd;

@property (nonatomic, weak) UIView *backView;


@end


@implementation XDProductsIntroduceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self = [[NSBundle mainBundle] loadNibNamed:@"XDProductsIntroduceCell" owner:self options:nil].firstObject;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)productsIntroduceCellWithTitle:(NSString *)title nearBackTimeLb: (NSString *)nearBackTime products:(NSString *)products{
    
    self.titleLb.text = title;
    self.nearBackTimeLb.text = nearBackTime;
    if (self.isVisible && self.tag == 1) {
            self.nextInd.transform = CGAffineTransformMakeRotation(M_PI_2);
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 40, kScreenW, 100)];
            view.backgroundColor = [UIColor whiteColor];
            self.backView = view;
            UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kScreenW - 30, 90)];
            backView.backgroundColor = kColorRGB(251,249,242);
            backView.layer.cornerRadius = 10;
            backView.layer.masksToBounds = YES;
            [view addSubview:backView];
        
        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, backView.frame.size.width - 30, 70)];
        textView.editable = NO;
        textView.font = kBaseFont_(12);
        textView.delegate = self;
        textView.textColor = kColorRGB(51, 51, 51);
        
        NSString *productStr = [products stringByAppendingString:@"查看原标的"];
        NSMutableAttributedString *mutAttr = [[NSMutableAttributedString alloc] initWithString:productStr];
       
        [mutAttr addAttribute:NSLinkAttributeName value:@"products" range:[productStr rangeOfString:@"查看原标的"]];
        textView.linkTextAttributes = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSForegroundColorAttributeName: kColorRGB(187,147,115)};
        textView.attributedText = mutAttr;
        textView.backgroundColor = [UIColor clearColor];
        [backView addSubview:textView];
        [self.contentView addSubview:view];
    } else {
        self.nextInd.transform = CGAffineTransformMakeRotation(0);
        self.backView = nil;
    }

  
}

- (IBAction)productsCellClick:(UIButton *)sender {
    
    self.isVisible = !self.isVisible;
    if ([self.delegate respondsToSelector:@selector(productsCellBtnClick:)]) {
        
        [self.delegate productsCellBtnClick:self];
        
    }
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    
    if ([URL.absoluteString isEqualToString:@"products"]) {
        if ([self.delegate respondsToSelector:@selector(productsLinkClick:)]) {
            [self.delegate productsLinkClick:self];
        }
        
        return NO;
    }
    return YES;
}

@end
